package com.atguigu.service.impl;

import com.atguigu.entity.PlatformPropertyKey;
import com.atguigu.entity.PlatformPropertyValue;
import com.atguigu.mapper.PlatformPropertyKeyMapper;
import com.atguigu.service.PlatformPropertyKeyService;
import com.atguigu.service.PlatformPropertyValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 属性表 服务实现类
 * </p>
 *
 * @author zhangqiang
 * @since 2021-08-23
 */
@Service
public class PlatformPropertyKeyServiceImpl extends ServiceImpl<PlatformPropertyKeyMapper, PlatformPropertyKey> implements PlatformPropertyKeyService {
    @Autowired
    PlatformPropertyValueService platformPropertyValueService;


    @Override
    public List<PlatformPropertyKey> getPlatformPropertyBySkuId(Long skuId) {
        return baseMapper.getPlatformPropertyBySkuId(skuId);
    }

    @Override
    public void savePlatformProperty(PlatformPropertyKey platformPropertyKey) {
        //判断属性内是否有id
        if (platformPropertyKey.getId()!= null){
            baseMapper.updateById(platformPropertyKey);
        }else{
            //保存数据到平台属性key
            baseMapper.insert(platformPropertyKey);
        }
        //将原value删除再查询添加
        QueryWrapper<PlatformPropertyValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("property_key_id", platformPropertyKey.getId());
        platformPropertyValueService.remove(queryWrapper);
        //保存数据到平台属性value
        List<PlatformPropertyValue> propertyValueList = platformPropertyKey.getPropertyValueList();
        //判断是否为空
        if (propertyValueList != null){
            for (PlatformPropertyValue platformPropertyValue : propertyValueList) {
                platformPropertyValue.setPropertyKeyId(platformPropertyKey.getId());
                platformPropertyValueService.save(platformPropertyValue);
            }
        }
    }

    @Override
    public List<PlatformPropertyKey> getPlatformPropertyByCategoryId(Long category1Id, Long category2Id, Long category3Id) {
//        //第一种方式性能不高
//        //先根据分类id查询平台属性key,先写sql
//        List<PlatformPropertyKey> platformPropertyKeys=baseMapper.getPropertyKeyByCategoryId(category1Id,category2Id,category3Id);
//        //再根据平台属性key的id查询平台属性值
//        for (PlatformPropertyKey platformPropertyKey : platformPropertyKeys) {
//            List<PlatformPropertyValue> propertValueByPropertyKeyId = platformPropertyValueService.getPropertValueByPropertyKeyId(platformPropertyKey.getId());
//            platformPropertyKey.setPropertyValueList(propertValueByPropertyKeyId);
//        }
//        return platformPropertyKeys;
        //第二种方式 推荐
        List<PlatformPropertyKey> platformPropertyKeyList= baseMapper.getPlatformPropertyByCategoryId(category1Id,category2Id,category3Id);
        return platformPropertyKeyList;
    }
}
